{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![QuantConnect Logo](https://cdn.quantconnect.com/web/i/qc_notebook_logo_rev0.png)\n",
    "## Welcome to The QuantConnect Research Page\n",
    "#### Refer to this page for documentation https://www.quantconnect.com/docs/research/overview#\n",
    "#### Contribute to this template file https://github.com/QuantConnect/Lean/blob/master/Research/BasicQuantBookTemplate.ipynb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## QuantBook Basics\n",
    "\n",
    "### Start QuantBook\n",
    "- Add the references and imports\n",
    "- Create a QuantBook instance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load in our startup script, required to set runtime for PythonNet\n",
    "%run ../start.py     # %run start.py # in Dev Container"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create an instance\n",
    "qb = QuantBook()\n",
    "\n",
    "# Select asset data\n",
    "spy = qb.AddEquity(\"SPY\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Historical Data Requests\n",
    "\n",
    "We can use the QuantConnect API to make Historical Data Requests. The data will be presented as multi-index pandas.DataFrame where the first index is the Symbol.\n",
    "\n",
    "For more information, please follow the [link](https://www.quantconnect.com/docs#Historical-Data-Historical-Data-Requests)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Gets historical data from the subscribed assets, the last 360 datapoints with daily resolution\n",
    "h1 = qb.History(qb.Securities.Keys, 360, Resolution.Daily)\n",
    "\n",
    "# Plot closing prices from \"SPY\" \n",
    "h1.loc[\"SPY\"][\"close\"].plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Indicators\n",
    "\n",
    "We can easily get the indicator of a given symbol with QuantBook. \n",
    "\n",
    "For all indicators, please checkout QuantConnect Indicators [Reference Table](https://www.quantconnect.com/docs#Indicators-Reference-Table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Example with BB, it is a datapoint indicator\n",
    "# Define the indicator\n",
    "bb = BollingerBands(30, 2)\n",
    "\n",
    "# Gets historical data of indicator\n",
    "bbdf = qb.Indicator(bb, \"SPY\", 360, Resolution.Daily)\n",
    "\n",
    "# drop undesired fields\n",
    "bbdf = bbdf.drop('standarddeviation', 1)\n",
    "\n",
    "# Plot\n",
    "bbdf.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
